GBML manual / Maths

sign

-1 when value is negative, 1 when it is positive, 0 when it is exactly 0.

Syntax

sign(value) -> real

Arguments

NameType
valuereal

Returns

real

Description

-1 when value is negative, 1 when it is positive, 0 when it is exactly 0. sign answers "which way," with no opinion at all about "how much."

Given a whole number, sign hands back a whole number; given a decimal, it hands back a decimal. sign_int does the same for whole numbers but refuses a decimal outright.

Example

hsp = sign(xstart - x) * 2;

Nudges this object 2 pixels toward wherever it spawned, every single frame, no matter how far away that is. This is the thing to notice about sign: the step size (2, here) is entirely up to the caller and never shrinks as the distance closes - so once this object is within 2 pixels of xstart, it overshoots and starts stepping back and forth across it forever, unless something else stops it. approach exists specifically to avoid that - it lands exactly on the target and stays there.

When to reach for this instead of approach

sign is the right tool when the STEP size itself should never change with distance - accelerating an object by a fixed amount every frame toward wherever a target currently is, for instance, rather than sliding smoothly to a stop beside it. approach is the right tool the moment "land exactly on the target and stop" is part of what is being asked for.

Cost

Two comparisons, hand-written - there is nothing in this file's usual "wraps a call into the Game Boy Advance module" family to reuse here, but it is still as cheap as this group gets.

On the GBA

Hand-written (two comparisons) - unlike almost everything else in this group, there is no single GBA-module call this wraps. Useful for "which way" without caring "how much": hsp += sign(target_x - x) moves one step toward target_x every frame regardless of how far away it is, where approach (below) would be the right call whenever the STEP size itself should also matter. Given a whole number it hands back a whole number; given a decimal it hands back a decimal. sign_int is the same call, for when you want a decimal refused outright.

See also: absapproachsign_int